package com.example.luyennghe;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
    public static final String bainghe_bookmark ="bainghe_bookmark";
    public static final String bainghe_dalam ="bainghe_dalam";
    public static final String diem = "diem";
    public static final String ngaylambai = "ngaylambai";  
   
    public static final String mail = "mail";
    public static final String password ="password";   
    
    public static final String DB_NAME = "userinfo.sqlite";
    public static final String DB_TABLE_USER = "user";
    public static final int DB_VERSION = 2;
    
    private final Context mContext;
    private SQLiteDatabase mDB;
    private DBHelper mDBHelper;
    private String path;
    public DBAdapter(Context c){
        mContext=c;
    }
    public static class DBHelper extends SQLiteOpenHelper{
        public DBHelper(Context context, String name, CursorFactory factory,
                int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            try
            {
                
            }
            catch(SQLException ex)
            {
                ex.printStackTrace();
            }
        }
 
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            Log.i("DBAdapter", "Updating database...");
            db.execSQL("DROP TABLE IF EXISTS Student");
            onCreate(db);
        }
    }

    public void openDB() throws SQLException
    {
        path = "/data/data/"+mContext.getPackageName()+"/"+DB_NAME; 
        File f = new File(path);
        if(!f.exists())
        {
            Log.e("DB","File khong ton tai");
            try {
                copyDB(mContext.getAssets().open(DB_NAME),new FileOutputStream(path));
                mDB=SQLiteDatabase.openDatabase(path, null,SQLiteDatabase.OPEN_READWRITE);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        else
            mDB=SQLiteDatabase.openDatabase(path, null,SQLiteDatabase.OPEN_READWRITE);
    }
    public void copyDB(InputStream inputStr, OutputStream outputStr) throws IOException{
        byte[] buffer = new byte[1024];
        int lenght;
        while((lenght = inputStr.read(buffer))>0){
            outputStr.write(buffer, 0, lenght);
        }
        inputStr.close();
        outputStr.close();
    }
    public void closeDB()
    {
        mDBHelper.close();
    }
    
    
    //them user
    public long themuser(String _mail, String _password)
    {
        
        ContentValues cv = new ContentValues();        
        cv.put(mail, _mail);
        cv.put(password, _password);
        return mDB.insert(DB_TABLE_USER, null, cv);
    }
    
    //check user
   public Cursor checkuser(String _mail, String _password)
    {
        Cursor mCursor = mDB.query(true, DB_TABLE_USER, new String[]{mail,password}, mail + "=? and " + password + "=?", new String[] {_mail, _password },null, null, null, null);
        if(mCursor != null)
        {
             mCursor.moveToFirst();
        }
        return mCursor;
    }
    
    // check bookmark
    public Cursor checkbookmark(String _tablename, String _bainghe) {
        Cursor mCursor = mDB.query(true, _tablename,
                new String[] { bainghe_bookmark }, bainghe_bookmark + "="
                        + _bainghe, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    //them bookmark
    public long thembookmark(String _tablename, String _bainghe_bookmark){
        ContentValues cv = new ContentValues();        
        cv.put(bainghe_bookmark, _bainghe_bookmark);
        return mDB.insert(_tablename, null, cv);
    }
    //xoa bookmark
    public long xoabookmark(String _tablename, String _bainghe_bookmark){
        return mDB.delete(_tablename, bainghe_bookmark + "= ?", new String[]{_bainghe_bookmark});
    }
    
    //them bai da lam
    public long thembainghedalam(String _tablename, String _bainghedalam, String _diem, String _ngaylambai){
        ContentValues cv = new ContentValues();        
        cv.put(bainghe_dalam, _bainghedalam);
        cv.put(diem, _diem);
        cv.put(ngaylambai, _ngaylambai);
        return mDB.insert(_tablename, null, cv);
    }
    //check bai nghe da lam
    public Cursor checkbainghedalam(String _tablename, String _bainghe)
    {
        Cursor mCursor = mDB.query(true, _tablename, new String[]{diem, ngaylambai}, bainghe_dalam + "=" + _bainghe, null ,null, null, null, null);
        if(mCursor != null)
        {
             mCursor.moveToFirst();
        }
        return mCursor;
    } 
}
